package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyDiscountCouponAdapter;
import com.kkd.kkdapp.adapter.MyDiscountCouponAdapter.OnTenderItemtListener;
import com.kkd.kkdapp.adapter.TenderAffirmDiscountCouponAdapter;
import com.kkd.kkdapp.adapter.TenderAffirmDiscountCouponAdapter.OnItemCliclistener;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.DiscountCoupon;
import com.kkd.kkdapp.http.entity.MyDiscountCouponData;
import com.kkd.kkdapp.http.entity.MyDiscountCouponDataList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.Animator.AnimatorListener;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 投标选择优惠券
 * 
 * @author mty
 * 
 */
public class TenderAffirmDiscountCouponActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private final int CODE_ZREO = 1;
	private final int CODE_ELSE = 2;

	private RelativeLayout deal_top_img; // 后退
	private TextView regulations; // 说明
	private PopupWindow pop;
	// private String version;
	private List<MyDiscountCouponDataList> mDataList;
	private List<MyDiscountCouponDataList> reDataList;
	private KkdXlistView mListView;
	private MyDiscountCouponAdapter adapter;
	private RelativeLayout nonuse; // 不使用优惠券
	private RelativeLayout myticket; // 选择优惠券
	private TextView deal_top_tv;
	private int pageNO = 1;
	private int pageSize = 10;
	private String mCouponType = "";
	private boolean REFRESHFLAG = true;
	private ImageView mNullImg;
	private TextView mNullTv;
	private int mPosition;
	private ListView gridview;
	private View view_bottom;
	private int integeFlag = 0;
	private String mCouponId;
	private String businessType;
	private TenderAffirmDiscountCouponAdapter mAdapter;
	private String bidId;
	private int mPageNO;
	private int mPageCount;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {

			case CODE_ZREO:
				if (mPageNO > mPageCount && mPageCount != 0) {
					mListView.stopLoadMore(-1);
					if (REFRESHFLAG) {
						mListView.stopLoadMore(1);
					}
					
				} else {

					if (pageNO == 1) {
						mDataList.clear();
						mListView.setNoMore(false);
					}

					mDataList.addAll(reDataList);
					adapter.notifyDataSetChanged();

					if (reDataList.size() > 0) {
						mListView.setVisibility(View.VISIBLE);
						
					}
					if (reDataList.size() < pageSize) {
						mListView.stopLoadMore(1);
					} else {
						mListView.stopLoadMore(0);
					}
					mListView.stopRefresh();
					Time_Date time_Date = new Time_Date();
					String time = time_Date.get_Year_Day();
					mListView.setRefreshTime(time);
				}
				if (REFRESHFLAG) {
					if (mDataList == null) {
						if (mDataList.size() == 0) {
							mNullImg.setVisibility(View.VISIBLE);
							mNullTv.setVisibility(View.VISIBLE);
							mListView.setFooterVisibile(View.GONE);
							if ("".equals(mCouponType)) {
								mNullTv.setText("暂无优惠券");
							} else if ("1".equals(mCouponType)) {
								mNullTv.setText("暂无抵现券");
							} else if ("2".equals(mCouponType)) {
								mNullTv.setText("暂无加息券");
							} else if ("3".equals(mCouponType)) {
								mNullTv.setText("暂无返现券");
							} else if ("4".equals(mCouponType)) {
								mNullTv.setText("暂无提现券");
							}
						}
					} else {
						if (mDataList.size() == 0) {
							mNullImg.setVisibility(View.VISIBLE);
							mNullTv.setVisibility(View.VISIBLE);
							mListView.setFooterVisibile(View.GONE);
							if ("".equals(mCouponType)) {
								mNullTv.setText("暂无优惠券");
							} else if ("1".equals(mCouponType)) {
								mNullTv.setText("暂无抵现券");
							} else if ("2".equals(mCouponType)) {
								mNullTv.setText("暂无加息券");
							} else if ("3".equals(mCouponType)) {
								mNullTv.setText("暂无返现券");
							} else if ("4".equals(mCouponType)) {
								mNullTv.setText("暂无提现券");
							}
							// mListView.setPullLoadEnable(false);
						} else if (mDataList.size() > 0) {
							mNullImg.setVisibility(View.GONE);
							mNullTv.setVisibility(View.GONE);
							mListView.setFooterVisibile(View.VISIBLE);
						}
					}
				}
				break;
			case CODE_ELSE:
				mListView.stopRefresh();
				mListView.initState();
				if (mDataList == null) {
					if (mDataList.size() == 0) {
						mNullImg.setVisibility(View.VISIBLE);
						mNullTv.setVisibility(View.VISIBLE);
						mListView.setFooterVisibile(View.GONE);
						if ("".equals(mCouponType)) {
							mNullTv.setText("暂无优惠券");
						} else if ("1".equals(mCouponType)) {
							mNullTv.setText("暂无抵现券");
						} else if ("2".equals(mCouponType)) {
							mNullTv.setText("暂无加息券");
						} else if ("3".equals(mCouponType)) {
							mNullTv.setText("暂无返现券");
						} else if ("4".equals(mCouponType)) {
							mNullTv.setText("暂无提现券");
						}
					}
				} else {
					if (mDataList.size() == 0) {
						mNullImg.setVisibility(View.VISIBLE);
						mNullTv.setVisibility(View.VISIBLE);
						mListView.setFooterVisibile(View.GONE);
						if ("".equals(mCouponType)) {
							mNullTv.setText("暂无优惠券");
						} else if ("1".equals(mCouponType)) {
							mNullTv.setText("暂无抵现券");
						} else if ("2".equals(mCouponType)) {
							mNullTv.setText("暂无加息券");
						} else if ("3".equals(mCouponType)) {
							mNullTv.setText("暂无返现券");
						} else if ("4".equals(mCouponType)) {
							mNullTv.setText("暂无提现券");
						}
					} else if (mDataList.size() > 0) {
						mNullImg.setVisibility(View.GONE);
						mNullTv.setVisibility(View.GONE);
						mListView.setFooterVisibile(View.VISIBLE);
					}
				}
				break;
			default:
				break;
			}
		}
	};
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tender_affirm_discount_coupon);
		initView();
		getMessage();
		setListener();
	}

	/**
	 * 初始化组件
	 */
	public void initView() {
		deal_top_img = (RelativeLayout) this.findViewById(R.id.deal_top_img);
		regulations = (TextView) this.findViewById(R.id.regulations);
		mListView = (KkdXlistView) this.findViewById(R.id.deal_detail_list);
		if (mDataList == null) {
			mDataList = new ArrayList<MyDiscountCouponDataList>();
		}
		nonuse = (RelativeLayout) this.findViewById(R.id.nonuse);
		myticket = (RelativeLayout) this.findViewById(R.id.myticket);
		deal_top_tv = (TextView) this.findViewById(R.id.deal_top_tv);
		mNullImg = (ImageView) this.findViewById(R.id.id_no_content);
		mNullTv = (TextView) this.findViewById(R.id.id_no_content_text);
		gridview = (ListView) findViewById(R.id.gridview);
		view_bottom = this.findViewById(R.id.view_bottom);
		mListView.setKKDScrollListener(this);
		listView = mListView.mListView;
		listView.setDividerHeight(0);
	}

	public void setListener() {
		myticket.setOnClickListener(this);
		deal_top_img.setOnClickListener(this);
		regulations.setOnClickListener(this);
		nonuse.setOnClickListener(this);
		view_bottom.setOnClickListener(this);
		adapter.setOnTenderItemtListener(new OnTenderItemtListener() {

			@Override
			public void onPosition(int id) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				intent.putExtra("mId", id);
				MyDiscountCouponDataList mMyDiscountCouponDataList = mDataList
						.get(id);
				bundle.putSerializable("seri", mMyDiscountCouponDataList);
				intent.putExtras(bundle);
				setResult(700, intent);
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.deal_top_img: // 后退
			finish();
			break;
		case R.id.regulations: // 使用规则
			showPopup(v);
			break;
		case R.id.nonuse: // 不使用优惠券
			adapter.mPosition = MyDiscountCouponAdapter.NOT_UESE;
			adapter.notifyDataSetChanged();
			setResult(600);
			finish();
			break;
		case R.id.myticket: // 选择优惠券
			if (integeFlag % 2 == 0) {
				mAdapter.notifyDataSetChanged();
				showWindow();
			} else {
				noshowWindow();
			}
			integeFlag++;
			break;
		case R.id.view_bottom:
			if (integeFlag % 2 != 0) {
				noshowWindow();
				integeFlag++;
			}
			break;
		}

	}

	public void getMessage() {
		Intent intent = this.getIntent();
		mPosition = intent.getIntExtra("mId", -3);
		mCouponId = intent.getStringExtra("mCouponId");
		businessType = intent.getStringExtra("businessType");
		bidId = intent.getStringExtra("bidId");
		adapter = new MyDiscountCouponAdapter(this, true, mDataList, mPosition,
				mCouponId);
		listView.setAdapter(adapter);
		mListView.startRefresh();
		createWindow();
	}

	@Override
	protected void onResume() {
		super.onResume();
		couponList();
	}

	/**
	 * 使用规则
	 */
	public void showPopup(View v) {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.regulations_popupwindow, null);
		pop = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT, false);
		pop.setOutsideTouchable(true);
		RelativeLayout close_tv = (RelativeLayout) view
				.findViewById(R.id.close_tv);
		pop.showAtLocation(v, 0, 0, 0);
		close_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				pop.dismiss();
				final WindowManager.LayoutParams attrs = getWindow()
						.getAttributes();
				attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
				getWindow().setAttributes(attrs);
				getWindow().clearFlags(
						WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
			}
		});
	}

	/**
	 * 优惠券
	 */
	public void couponList() {
		String coustomerIdAndToken = PreferenceUtils.getInstance()
				.getcustomerId(this);
		String[] cat = coustomerIdAndToken.split("&");
		if (cat == null || cat.length == 0)
			return;
		KkdHttpMagager.getInstance().getMyDiscountManager(bidId, mCouponType,
				"" + businessType, "", 
				pageNO, pageSize, 0, MyDiscountCouponData.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						MyDiscountCouponData mMyDiscountCouponData = (MyDiscountCouponData) responseBody;
						mPageNO = mMyDiscountCouponData.getPageNO();
						mPageCount = mMyDiscountCouponData.getPageCount();
						reDataList = mMyDiscountCouponData.getDataList();
						handler.sendEmptyMessage(CODE_ZREO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						showToast(desc);
						handler.sendEmptyMessage(CODE_ELSE);
//						handler.postDelayed(new Runnable() {
//
//							@Override
//							public void run() {
//								showToast("加载失败");
//								mListView.stopLoadMore(1);
//								mListView.stopRefresh();
//								if (mDataList.size() == 0) {
//									mNullImg.setVisibility(View.VISIBLE);
//									mNullTv.setVisibility(View.VISIBLE);
//									mListView.setFooterVisibile(View.GONE);
//									mNullTv.setText("暂无内容");
//								}
//							}
//						}, 1000);
					}
				});

	}

	public void createWindow() {
		List<DiscountCoupon> sList = new ArrayList<DiscountCoupon>();
		if ("3".equals(businessType)) {
			sList.add(new DiscountCoupon("全部", R.drawable.complete_img));
			sList.add(new DiscountCoupon("抵现券", R.drawable.deposit_img));
			sList.add(new DiscountCoupon("返现券", R.drawable.negative_img));
		} else {
			sList.add(new DiscountCoupon("全部", R.drawable.complete_img));
			sList.add(new DiscountCoupon("抵现券", R.drawable.deposit_img));
			sList.add(new DiscountCoupon("加息券", R.drawable.raise_img));
			sList.add(new DiscountCoupon("返现券", R.drawable.negative_img));
		}
		gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mAdapter = new TenderAffirmDiscountCouponAdapter(
				TenderAffirmDiscountCouponActivity.this);
		mAdapter.setonItemClickListener(new OnItemCliclistener() {

			@Override
			public void onPosition(int id) {
				pageNO = 1;
				mListView.setNoMore(false);
				switch (id) {
				case 0:
					deal_top_tv.setText("选择优惠券");
					mCouponType = "";
					couponList();
					if (integeFlag % 2 != 0) {
						noshowWindow();
						integeFlag++;
					}
					break;
				case 1:
					deal_top_tv.setText("抵现券");
					mCouponType = "1";
					couponList();
					if (integeFlag % 2 != 0) {
						noshowWindow();
						integeFlag++;
					}
					break;
				case 2:
					if ("3".equals(businessType)) {
						deal_top_tv.setText("返现券");
						mCouponType = "3";
						couponList();
						if (integeFlag % 2 != 0) {
							noshowWindow();
							integeFlag++;
						}
					} else {
						deal_top_tv.setText("加息券");
						mCouponType = "2";
						couponList();
						if (integeFlag % 2 != 0) {
							noshowWindow();
							integeFlag++;
						}
					}
					break;
				case 3:
					if ("3".equals(businessType)) {
						deal_top_tv.setText("选择优惠券");
						mCouponType = "";
						couponList();
						if (integeFlag % 2 != 0) {
							noshowWindow();
							integeFlag++;
						}
					} else {
						deal_top_tv.setText("返现券");
						mCouponType = "3";
						couponList();
						if (integeFlag % 2 != 0) {
							noshowWindow();
							integeFlag++;
						}
					}
					break;
				}

			}
		});
		mAdapter.appendData(sList, true);
		gridview.setAdapter(mAdapter);
	}

	public void showWindow() {
		showAnimTranslate(gridview);
		showAnimTranslateup(view_bottom);
	}

	public void noshowWindow() {
		noshowAnimTranslate(gridview);
		noshowAnimTranslateup(view_bottom);
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		couponList();
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		couponList();
	}

	public void showAnimTranslate(final View view) {
		ObjectAnimator animation;
		PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(
				"translationY", -1000, 0);
		animation = ObjectAnimator.ofPropertyValuesHolder(view, pvhY);
		animation.setDuration(300);
		animation.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {
				view.setVisibility(View.VISIBLE);

			}

			@Override
			public void onAnimationRepeat(Animator animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animator animation) {
			}

			@Override
			public void onAnimationCancel(Animator animation) {
				// TODO Auto-generated method stub

			}
		});
		animation.start();
	}

	public void noshowAnimTranslate(final View view) {
		ObjectAnimator animation;
		PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(
				"translationY", 0, -1000);
		animation = ObjectAnimator.ofPropertyValuesHolder(view, pvhY);
		animation.setDuration(300);
		animation.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animator animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animator animation) {
				view.setVisibility(View.GONE);
			}

			@Override
			public void onAnimationCancel(Animator animation) {
				// TODO Auto-generated method stub

			}
		});
		animation.start();
	}

	public void showAnimTranslateup(final View view) {
		ObjectAnimator animation;
		PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(
				"translationY", 10000, 0);
		animation = ObjectAnimator.ofPropertyValuesHolder(view, pvhY);
		animation.setDuration(300);
		animation.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {
				view.setVisibility(View.VISIBLE);

			}

			@Override
			public void onAnimationRepeat(Animator animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animator animation) {
			}

			@Override
			public void onAnimationCancel(Animator animation) {
				// TODO Auto-generated method stub

			}
		});
		animation.start();
	}

	public void noshowAnimTranslateup(final View view) {
		ObjectAnimator animation;
		PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(
				"translationY", 0, 10000);
		animation = ObjectAnimator.ofPropertyValuesHolder(view, pvhY);
		animation.setDuration(300);
		animation.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {

			}

			@Override
			public void onAnimationEnd(Animator animation) {
				view.setVisibility(View.GONE);
			}

			@Override
			public void onAnimationCancel(Animator animation) {
				// TODO Auto-generated method stub

			}
		});
		animation.start();
	}
}
